/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.header;

import javax.sip.InvalidArgumentException;
import javax.sip.header.AcceptEncodingHeader;
import java.text.ParseException;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class AcceptEncodingHeaderImpl extends ParametrizedHeaderBase implements AcceptEncodingHeader {
    /**
     * Encoding value.
     */
    private String encoding;

    private final static AcceptEncodingHeaderParser parser = new AcceptEncodingHeaderParser();

    protected HeaderParser getParser() {
        return parser;
    }

    public AcceptEncodingHeaderImpl() {
        super(AcceptEncodingHeader.NAME);
    }

    AcceptEncodingHeaderImpl(byte[] data, int start, int end) {
        super(AcceptEncodingHeader.NAME, data, start, end);
    }

    public Object clone() {
        AcceptEncodingHeaderImpl header = new AcceptEncodingHeaderImpl();
        header.cloneBase(this);
        if (parsed) {
            header.cloneParameters(this);
            header.encoding = encoding;
        }
        return header;
    }

    /**
     * Gets q-value of the encoding in this encoding value. A value of
     * <code>-1</code> indicates the<code>q-value</code> is not set.
     *
     * @return q-value of encoding value, -1 if q-value is not set.
     */
    public float getQValue() {
        final String qValue = getParameter("q");
        if (qValue != null)
            return Float.parseFloat(qValue);
        else
            return 0.0f;
    }

    /**
     * Sets q-value for the encoding in this encoding value. Q-values allow the
     * user to indicate the relative degree of preference for that encoding,
     * using the qvalue scale from 0 to 1. If no q-value is present, the
     * encoding should be treated as having a q-value of 1.
     *
     * @param qValue - the new float value of the q-value, a value of -1 resets
     *               the qValue.
     * @throws InvalidArgumentException if the q parameter value is not
     *                                  <code>-1</code> or between <code>0 and 1</code>.
     */
    public void setQValue(float qValue) throws InvalidArgumentException {
        try {
            setParameter("q", Float.toString(qValue));
        } catch (ParseException e) {
            throw new InvalidArgumentException("Cannot set the q-value!", e);
        }
    }

    /**
     * Sets the encoding of an EncodingHeader.
     *
     * @param encoding - the new string value defining the encoding.
     * @throws ParseException which signals that an error has been reached
     *                        unexpectedly while parsing the encoding value.
     */
    public void setEncoding(String encoding) throws ParseException {
        this.encoding = encoding;
    }

    /**
     * Gets the encoding of an EncodingHeader. Returns null if no
     * encoding is defined in an EncodingHeader.
     *
     * @return the string value identifing the encoding
     */
    public String getEncoding() {
        return encoding;
    }

    /**
     * Returns string representation of the header's value.
     *
     * @return The string representation of the header's value.
     */
    protected String getValueAsString() {
        return encoding;
    }
}
/*
 * $Log$
 */